package com.lksoft.bz.action;

import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import com.lksoft.bz.BeadUsage;
import com.lksoft.bz.Bz;
import com.lksoft.bz.command.SetLetterCommand;

public class SetLetterAction extends AbstractAction {
	private static final long serialVersionUID = -5635691580355305979L;
	private Bz bz;
	
	private BeadUsage targetBeadUsage;
	
	/**
	 * @param bz The program
	 * @throws IOException 
	 */
	public SetLetterAction(Bz bz) throws IOException{
		super();
		this.bz = bz;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// Display input dialog with current letter
		String userLetter = JOptionPane.showInputDialog(Bz
				.getUIString("messages.input.setletter"), "" + targetBeadUsage.getLetter());
		// User cancel
		if( userLetter == null ) return;
			
		// If no letter, unset
		char letterChoosen = '\0';
		if( !userLetter.isEmpty() ) letterChoosen = userLetter.charAt(0); 
		
		// Create command
		SetLetterCommand command =  new SetLetterCommand(bz, targetBeadUsage, targetBeadUsage.getLetter(), letterChoosen);
		// Execute
		command.redo();
		
		// Register
		bz.addCommand(command);
	}
	
	public void setBeadUsage(BeadUsage beadUsage){
		this.targetBeadUsage = beadUsage;
	}
}
